local Element = import("./Element.lua");

local Radio = commonlib.inherit(Element);

Radio:Property("Checked", false, "IsChecked");
Radio:Property("DefaultStyle", "width: 20px; height: 20px; padding: 2px;");

function Radio:ctor()
    self:SetName("Radio");
end

function Radio:Init(xmlnode, window, parent)
    Radio._super.Init(self, xmlnode, window, parent);
    return self;
end

function Radio:OnAttrValueChange(attrName, attrValue)
    if (attrName == "checked") then
        self:SetChecked(self:GetAttrBoolValue("checked"));
    end
end

function Radio:RenderContent()
    local x, y, w, h = self:GetContentPosition();
    local radius = math.min(w / 2, h / 2);

    self:SetPenColor(0xb5, 0xb5, 0xb5, 0xff);
    self:SetLineWidth(4);
    self:DrawArc(x + radius, y + radius, radius, 0, 360, false, true);

    if (self:IsChecked()) then 
        self:SetBrushColor(0x66, 0x66, 0x66, 0x66);
        self:DrawArc(x + radius, y + radius, radius - 6, 0, 360, true);
    end
end

function Radio:OnMouseDown(event)
    if (event:IsLeftButton()) then
        self:SetChecked(not self:IsChecked());
    end
end

return Radio;